//editables on first profile page
$.fn.editable.defaults.mode = 'inline';
$.fn.editableform.loading = "<div class='editableform-loading'><i class='light-blue icon-2x icon-spinner icon-spin'></i></div>";
$.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="icon-ok icon-white"></i></button>' +
    '<button type="button" class="btn editable-cancel"><i class="icon-remove"></i></button>';

createEditor();
function createEditor() {
    //enableBtn();
    UE.getEditor('newsContent');
}

var newsInfo;
var newsId = $("#newsId").val();

initNewsInfo();
function initNewsInfo() {
    if (cochoApp.isNotEmpty(newsId)) {
        getNewsInfo();
        if (cochoApp.isNotEmpty(newsInfo)) {
            $("#newsTitle").val(newsInfo.newsTitle);
            $("#category").val(newsInfo.category);
            $("#newsContent").val(newsInfo.newsContent);
        }
    }
}

function getNewsInfo() {
    $.ajax({
        type: "post",
        async: false,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/news/getNewsInfoById.do?token=" + cochoApp.getTokenByStorage() + "&newsId=" + newsId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    newsInfo = data.dataRows[0];
                } else {
                    cochoAlert.notificationsWarn("暂无新闻信息！");
                }
            } else {
                cochoAlert.notificationsWarn("获取新闻信息失败！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

var newsLbList;//新闻公告类别
initnewsLbSelect();
function initnewsLbSelect() {
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/members/common/getXwlbInfo.do?token=" + cochoApp.getTokenByStorage(),
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    newsLbList = data.dataRows;
                    for (var i = 0; i < newsLbList.length; i++) {
                        var codelistInfo = newsLbList[i];
                        if (newsInfo != null && codelistInfo.codevalue == newsInfo.category) {
                            $("#category").append("<option selected='selected' value='" + codelistInfo.codevalue + "'>" + codelistInfo.codename + "</option>");
                        } else {
                            $("#category").append("<option value='" + codelistInfo.codevalue + "'>" + codelistInfo.codename +  "</option>");
                        }
                    }
                    $("#category").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

function saveNewsInfo() {
    var result = checkNullAndWarnHasNull();
    if (result == true) {//如果必填项都不为空
        var newsId = $("#newsId").val();
        var newsTitle = $("#newsTitle").val();
        var category = $("#category").val();
        var newsContent = UE.getEditor('newsContent').getContent();
        var paramData = {"newsId":newsId,"newsTitle": newsTitle,"category":category,"newsContent": newsContent, "token": cochoApp.getTokenByStorage()};
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/admin/news/saveNewsInfo.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    cochoAlert.notificationsWarn(data.statusMsg);
                    $("#newsId").val(data.content);
                } else {
                    cochoAlert.notificationsWarn("保存失败！");
                    return false;
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
                return false;
            }
        });
    }
}
